package com.example.week2_try2;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RateItemTask implements Runnable{
    private static final String TAG = "RateItemTask";
    private Handler handler;
    //private ArrayList<HashMap<String,String>> listItems;
    private String logDate = "";
    private final String DATE_SP_KEY = "lastRateDateStr";

    public void setHandler(Handler handler){
        this.handler = handler;
    }

    @Override
    public void run() {
        List<String> list2 = new ArrayList<String>();
        //listItems = new ArrayList<HashMap<String,String>>();
        List<Item> listItems=new ArrayList<Item>();
        try {
            Document doc = null;
            doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/").get();
            Log.i(TAG, "run: "+doc.title());
            Elements tables=doc.getElementsByTag("table");
            //Log.i(TAG, "run: tables="+tables);
            Element secondTable=tables.get(1);//单位元素--获取第二个table
            Log.i(TAG, "run: table="+secondTable);
            //通过行获取数据
            Elements trs=secondTable.getElementsByTag("tr");
            trs.remove(0);
            for(Element tr:trs){
                Log.i(TAG, "run: tr="+tr);
                //从行中获得td元素
                Elements tds=tr.getElementsByTag("td");
                //获取第一列
                Element td1=tds.get(0);
                Element td2=tds.get(4);
                Log.i(TAG, "run: td1="+td1.text()+"\n run: td2="+td2.text());
                if(!(td2.text().equals(""))){
                    HashMap<String,String> map = new HashMap<String,String>();//map一定要在循环里创建，在外面的话就是同一个map不停的放进list

                    float rate = 100f/Float.parseFloat(td2.text());//计算汇率
                    Item item = new Item(td1.text(),String.valueOf(rate));
                    listItems.add(item);
//                    list2.add(td1.text()+"------>"+String.valueOf(rate));
//                    map.put("ItemTitle",td1.text());//标题文字
//                    map.put("ItemDetail",String.valueOf(rate));//详情描述
                    //listItems.add(map);
                }
                else{
                    HashMap<String,String> map = new HashMap<String,String>();//map一定要在循环里创建，在外面的话就是同一个map不停的放进list
                    Item item = new Item(td1.text(),td2.text());
                    listItems.add(item);
//                    map.put("ItemTitle",td1.text());//标题文字
//                    map.put("ItemDetail",td2.text());//详情描述
                    //listItems.add(map);
//                    list2.add(td1.text()+"------>"+td2.text());
                }
            }
            Message msg=handler.obtainMessage(9,listItems);//括号内可写what
            handler.sendMessage(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
